package com.trkj.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.jpa.LmbConsumptiveStockDAO;
import com.trkj.dao.jpa.LmbStorehouseDAO;
import com.trkj.pojos.LmbConsumptiveStock;
import com.trkj.pojos.YzrStorehouse;
import com.trkj.vo.LmbCkVo;

@Service
@Transactional
public class LmbKcCunService {
	@Autowired
	private LmbConsumptiveStockDAO stockDAO;
	@Autowired
	private LmbStorehouseDAO storehouseDAO;
	
	public Map<String,Object>  queryAllKncunByPage(int page,int rows){
		PageRequest pr = new PageRequest(page - 1,rows);
		//获取当前页的记录
		List<LmbConsumptiveStock> list = stockDAO.findAllKucun(pr);
		long total = stockDAO.countfindAllKucun();
		Map<String,Object> map = new HashMap<>();
		map.put("total", total);
		map.put("rows", ckpoListToVoList(list));
		return map;		
	}
	
	public LmbCkVo ckpoToVo(LmbConsumptiveStock stock){
		LmbCkVo ckVo = new LmbCkVo();
		ckVo.setCstockId(stock.getCstockId());
		ckVo.setCstocBatchNum(stock.getCstocBatchNum());
		ckVo.setCstocCid(stock.getCstocCid());
		ckVo.setCstocCname(stock.getCstocCname());
		ckVo.setCstocNorms(stock.getCstocNorms());
		ckVo.setCstocNum(stock.getCstocNum());
		ckVo.setCstockSupplier(stock.getCstockSupplier());
		ckVo.setCstocWarehouseId(stock.getCstocWarehouseId());
		ckVo.setCstockProductDate(stock.getCstockProductDate());
		ckVo.setCstockValidTill(stock.getCstockValidTill());
		System.err.println(stock.getCstocWarehouseId());
		String ckname = storehouseDAO.findOne(stock.getCstocWarehouseId()).getStoreName();
		ckVo.setCkName(ckname);
		return ckVo;		
	}
	
	public List<LmbCkVo> ckpoListToVoList(List<LmbConsumptiveStock> list){
		if(list==null) return null;
		List<LmbCkVo> ckVo = new ArrayList<>();
		for(LmbConsumptiveStock stock:list){
			LmbCkVo CkVo = ckpoToVo(stock);
			ckVo.add(CkVo);
		}
		return ckVo;		
	}
	
	public List<LmbCkVo> queryAllKncun(){
		List<LmbConsumptiveStock> list = (List<LmbConsumptiveStock>) stockDAO.findAll();
		return ckpoListToVoList(list);		
	}
	
	public Map<String,Object>  queryAllByckName(int page,int rows,String ckName){
		PageRequest pr = new PageRequest(page - 1,rows);
		List<YzrStorehouse> house = storehouseDAO.findStoreByckName(ckName);
		List<LmbConsumptiveStock> list = new ArrayList<>(); 
		long total =0l;
		int i=1;
		for(YzrStorehouse storehouse:house){
			List<LmbConsumptiveStock> stocklist = stockDAO.findKucunByckName(pr, storehouse.getStoreId());
			System.out.println("i:"+i);
			list.addAll(stocklist);
			long t= stockDAO.countfindKucunByckName(storehouse.getStoreId());
			total = total+t;
			i++;
		}
		Map<String,Object> map = new HashMap<>();
		map.put("total", total);
		map.put("rows", ckpoListToVoList(list));
		return map;		
	}
	
}
